﻿/********************************
------------	utils library	--------------
********************************/
var Utils = function(){};
Utils.tagRegExp = /<\s*(\w+[0-9]?)\s*>/g;
Utils.htmlLowerCase = function(_html){
	if(!_html){
		return ;
	}
	if(!Utils.tagRegExp.test(_html)){
		return _html;
	}
	return _html && _html.replace(Utils.tagRegExp, function(w){
		return w.toLowerCase();
	});
};
Utils.seriallize = function(obj){
	if(obj.constructor == String){
		return obj;
	}
	var  re = [];
	if(obj.constructor == Array){
		for(var k=0, l=obj.length; k<l; k++){
			re.push(obj[k].key + "=" + encodeURIComponent(obj[k].value));
		}
	}else if(obj.constructor == Object){
		for(var j in obj){
			re.push(j + "=" + encodeURIComponent(obj[j]));
		}
	}else{}
	return re.join("&");
};
Utils.strToJson = function(_str){
	if(_str.constructor == String){
		return eval("("+_str+")");
	}
	if(_str.constructor == Object){
		return _str;
	}
};
/********************************
----------	query library	--------------
********************************/
var zJQ = function(){};
/********************************
----------	event	--------------
********************************/
zJQ.addEvent = function(obj, type, fn){
	try{
		obj.addEventListener(type, fn);
	}catch(e){
		obj.attachEvent("on"+type, fn);
	}
};
zJQ.removeEvent = function(obj, type, fn){
	try{
		obj.removeEventListener(type, fn);
	}catch(e){
		obj.detachEvent("on"+type, fn);
	}
};
/********************************
----------	get DOM Elements	--------------
********************************/
zJQ.byId = function(_id){
	if(!_id){
		return document;
	}
	var re = document.getElementById(_id) || document;
	return re;
};
zJQ.byTag = function(_tag, _content){
	if(!_tag){
		return _content || document;
	}
	_content = _content || document;
	var re = _content.getElementsByTagName(_tag) || document;
	return re;
};
zJQ.byClass = function(_class, _content){
	if(!_class){
		return _content || document;
	}
	_content = _content || document;
	var aTmp = _content.getElementsByTagName("*"),
		  re = new Array();
	for(var k=0, len=aTmp.length; k<len; k++){
		if(aTmp[k].className == _class){
			re.push(aTmp[k]);
		}
	}
	return re;
};
/********************************
----------	get css style	-----------------
********************************/
zJQ.css = function(obj, style, value){
	if(!obj){
		return false;
	}
	if(arguments.length == 1){
		return obj.currentStyle?obj.currentStyle : document.defaultView.getComputedStyle(obj, false);
	}
	if(arguments.length == 2){
		return parseFloat(obj.currentStyle?obj.currentStyle[style] : document.defaultView.getComputedStyle(obj, false)[style]);
	}
	switch(style){
		case 'width':
		case 'height':
		case 'paddingLeft':
		case 'paddingTop':
		case 'paddingRight':
		case 'paddingBottom':
			value=Math.max(value,0);
			obj.style[style]=value+'px';
			break;
		case 'left':
		case 'top':
		case 'marginLeft':
		case 'marginTop':
		case 'marginRight':
		case 'marginBottom':
			obj.style[style]=value+'px';
			break;
		case 'opacity':
			obj.style.filter="alpha(opacity:"+value*100+")";
			obj.style.opacity=value;
			break;
		default:
			obj.style[style]=value;
			break;
	}
	return function (style_in, value_in){css(obj, style_in, value_in)};
};
/********************************
-----------	travel DOM Node	--------------
********************************/
zJQ.next = function(obj){
	if(!obj){
		return document;
	}
	do{
		obj = obj.nextSibling;
	}while(obj && obj.nodeType != 1);
	return obj;
};
zJQ.prev = function(obj){
	if(!obj){
		return document;
	}
	do{
		obj = obj.previousSibling;
	}while(obj && obj.nodeType != 1);
	return obj;
};
zJQ.first = function(obj){
	if(!obj){
		return document;
	}
	obj = obj.firstChild;
	return obj && obj.nodeType == 1? obj : zJQ.next(obj);
};
zJQ.last = function(obj){
	if(!obj){
		return document;
	}
	obj = obj.lastChild;
	return obj && obj.nodeType ==1 ? obj : zJQ.prev(obj);
};
zJQ.parent = function(obj, num){
	num = num || 1;
	for(var k=0; k<num; k++){
		if(obj != null){
			obj = obj.parentNode;
		}
	}
	return obj;
};
/***********************************
-----	get element text/html/attributes	-----
***********************************/
zJQ.text = function(obj, _txt){
	if(arguments.length == 2){
		obj.firstChild.nodeValue = _txt;
		return _txt;
	}
	var t = "";
	obj = obj.childNodes || obj;
	for(var k=0, l=obj.length; k<l; k++){
		t += obj[k].nodeType == 3 ? obj[k].nodeValue : zJQ.text(obj[k]);
	}
	return t;
};
zJQ.html = function(obj, _html){
	if(_html){
		obj.innerHTML = _html;
		return zJQ;
	}
	return Utils.htmlLowerCase(obj.innerHTML);
};
zJQ.attr = function(obj, _attr, _value){
	if(!_attr || _attr.constructor != String){
		return "";
	}
	_attr = {'for' : 'htmlFor', 'class': 'className'}[_attr] || _attr;
	if(typeof _value != 'underfined'){
		try{
			obj[_attr] = _value;
		}catch(e){
			if(obj.setAttribute){
				obj.setAttribute(_attr, _value);
			}
		}
	}
	return obj[_attr] || obj.getAttribute(_attr);
};
zJQ.hasClass = function(obj, className){
	var sClassStr = obj.getAttribute("className");
	if(sClassStr.test(className)){
		return true;
	}
	return false;
}
/***********************************
-------	change DOM structure	-----------
***********************************/